define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'company_stocks_confirm/index' + location.search,
                    // add_url: 'company_stocks_confirm/add',
                    // edit_url: 'company_stocks_confirm/edit',
                    // del_url: 'company_stocks_confirm/del',
                    // multi_url: 'company_stocks_confirm/multi',
                    import_url: 'company_stocks_confirm/import',
                    table: 'company_stocks_confirm',
                }
            });

            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                var extend = data.extend;
                var html = [];
                for (var i = 0; i < extend.length; i++) {
                    html.push(extend[i]["name"] + ":<span>【"+extend[i]["value"]+"】，</span>");
                }
                $(".extend").html(html.join(""));
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'company.name', title: __('Company.name'), operate: 'LIKE'},
                        {field: 'product.name', title: __('Product.name'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'before', title: __('Before')},
                        {field: 'changes', title: __('Changes')},
                        // {field: 'after', title: __('After')},
                        {field: 'confirm_type', title: __('Confirm_type'), searchList: {"1":__('Confirm_type 1'),"2":__('Confirm_type 2'),"0":__('未接收')}, formatter: Table.api.formatter.normal},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'check',
                                text: __('接收'),
                                icon: 'fa fa-check-square',
                                classname: 'btn btn-warning btn-xs btn-detail btn-dialog',
                                url: 'company_stocks_confirm/check',
                                hidden:function (row) {
                                    return row.confirm_type == 0 ? false : true;
                                }
                            },{
                                name: 'detail',
                                text: __('详情'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'company_stocks_confirm/detail'
                            }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-record", function () {
                Fast.api.open('company_stocks_confirm/record', "录入", {});
            });

            $(document).on("click", "#stock_detail", function () {
                Fast.api.open('company_stocks/details', "现有库存", {});
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        record: function () {
            require(['bootstrap-select', 'bootstrap-select-lang']);
            var renderselect = function (select, data) {
                var html = [];
                html.push("<option value=''>--请选择--</option>");
                for (var i = 0; i < data.length; i++) {
                    html.push("<option value='" + data[i].id + "'>" + data[i].title + "</option>");
                }
                $(select).html(html.join(""));
                select.trigger("change");
                if (select.data("selectpicker")) {
                    select.selectpicker('refresh');
                }
                return select;
            };

            $(document).on("change", "#c-product_id", function(){
                const val = $(this).val();
                Fast.api.ajax({
                    url: 'company_stocks_confirm/policyList',
                    data: {id: val},
                    loading: false
                }, function (data) {
                    $("#relation-zone .relation-item").remove();
                    renderselect($("#c-policy_id"), data);
                    return false;
                }, function () {
                    return false;
                });
            });

            $(document).on('change', ".types", function () {
                const val = $(this).val();
                if(val == 1){
                    $("#file_url").show();
                    $("#sn").hide();
                }else if(val == 2){
                    $("#file_url").hide();
                    $("#sn").show();
                }
            });
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        check: function () {
            Controller.api.bindevent();
            $(document).on('change', "input[name=confirm_type]", function () {
                const val = $(this).val();
                if(val == 1){
                    $("#remark").hide();
                }else if(val == 2){
                    $("#remark").show();
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
